﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.DAL.Estate
{
    public class AreaDAL
    {
        public static DataTable p_area_listall_table(int parentid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@parentid", parentid));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_area_listall", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

        public static List<AreaInfo> p_area_listall(int parentid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@parentid", parentid));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.fetch_procedure("p_area_listall", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            List<AreaInfo> list = new List<AreaInfo>();
            for (int i = 0; i < db.num_rows; i++)
            {
                AreaInfo c = new AreaInfo();
                c.DelFlag = (db.rows[i]["delFlag"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["delFlag"].ToString());
                c.Id = (db.rows[i]["id"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["id"].ToString());
                c.ParentId = (db.rows[i]["parentId"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["parentId"].ToString());
                c.Ord = (db.rows[i]["ord"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["ord"].ToString());
                c.Name = (db.rows[i]["name"] == System.DBNull.Value) ? "" : db.rows[i]["name"].ToString();
                c.NamePy = (db.rows[i]["namePy"] == System.DBNull.Value) ? "" : db.rows[i]["namePy"].ToString();
                list.Add(c);
            }
            return list;
        }
        public static List<AreaInfo> p_area_CityListAll(int parentid, string IdList, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@parentid", parentid));
            parms.Add(new SqlParameter("@cityid", IdList));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            db.fetch_procedure("p_area_CityListAll", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            List<AreaInfo> list = new List<AreaInfo>();
            for (int i = 0; i < db.num_rows; i++)
            {
                AreaInfo c = new AreaInfo();
                c.DelFlag = (db.rows[i]["delFlag"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["delFlag"].ToString());
                c.Id = (db.rows[i]["id"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["id"].ToString());
                c.ParentId = (db.rows[i]["parentId"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["parentId"].ToString());
                c.Ord = (db.rows[i]["ord"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i]["ord"].ToString());
                c.Name = (db.rows[i]["name"] == System.DBNull.Value) ? "" : db.rows[i]["name"].ToString();
                c.NamePy = (db.rows[i]["namePy"] == System.DBNull.Value) ? "" : db.rows[i]["namePy"].ToString();
                list.Add(c);
            }
            return list;
        }
        public static void p_area_save(int id, string name, string namePy, int parentId, int delFlag, int ord, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@id", id));
            parms.Add(new SqlParameter("@name", name));
            parms.Add(new SqlParameter("@namePy", namePy));
            parms.Add(new SqlParameter("@parentId", parentId));
            parms.Add(new SqlParameter("@delFlag", delFlag));
            parms.Add(new SqlParameter("@ord", ord));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.execute_procedure("p_area_save", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

        }

        public static void p_area_delete(int id, out string outmsg, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@id", id));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);


            db.execute_procedure("p_area_delete", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

        }

        public static string SelectAreaNameById(int id)
        {
            using (_Database db = new _Database("estate"))
            {
                string name = "";
                string sql = "select name from t_area where id=" + id + "";
                DataSet ds = db.Query(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    name = ds.Tables[0].Rows[0]["name"].ToString();
                }

                return name;
            }
        }

    }
}
